<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input
            v-model="search"
            placeholder="输入试题题目搜索"
            clearable
            @change="getTitleList">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getTitleList" class="el-icon-search" type="primary"></el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="确定批量删除?" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu" v-if="hasPermission()">删除选中</el-button>
        </el-popconfirm>
      </el-form-item>
      <el-form-item>
        <template>
          <el-select v-model="subject" clearable placeholder="按学科分类" @change="getTitleList">
            <el-option
                v-for="item in subject_list"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item>
        <template>
          <el-select v-model="title_type" clearable placeholder="按题型分类" @change="getTitleList">
            <el-option
                v-for="item in title_type_list"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column
          prop="id"
          label="id"
          width="70">
      </el-table-column>
      <el-table-column
          prop="theme"
          label="题干"
          width="500">
        <template slot-scope="scope">
          <p v-html="(scope.row.theme).slice(0, 38) + '...'"></p>
        </template>
      </el-table-column>
      <el-table-column
          prop="subject_name"
          label="学科"
          width="90">
      </el-table-column>
      <el-table-column
          prop="type_name"
          label="题型"
          width="80">
      </el-table-column>
      <el-table-column
          prop="title_score"
          label="分数"
          width="80">
      </el-table-column>
      <el-table-column
          prop="time"
          label="创编时间"
          width="170">
      </el-table-column>

      <el-table-column
          prop="icon"
          label="操作">
        <template slot-scope="scope">
          <el-button @click="editHandle(scope.row.id, scope.row.title_type)" size="mini">编辑</el-button>
          <el-button @click="showQuestion(scope.row.id)" size="mini">预览</el-button>
          <span>&nbsp;&nbsp;</span>
          <template>
            <el-popconfirm title="确定删除?" @confirm="delHandle(scope.row.id)">
              <el-button type="danger" slot="reference" v-if="hasPermission()" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>
    <el-dialog :visible.sync="questionShow.dialog" style="width: 100%;height: 100%">
      <ShowTitle :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading"/>
    </el-dialog>
    <!--    一直维护好ueditor-->
    <Ueditor v-show="false"></Ueditor>
    <div style="text-align: center; padding-bottom: 10px;">&COPY;copyleft@海饼干</div>
  </div>
</template>

<script>
import ShowTitle from "../../components/ShowTitle";
import Ueditor from '@/components/Ueditor'

export default {
  name: "TitleList",
  components: {ShowTitle, Ueditor},
  data() {
    return {
      questionShow: {
        qType: 0,
        dialog: false,
        question: {},
        loading: false
      },
      title_type: null,
      subject: null,
      title_type_list: [
        {id: 0, name: '单选题'},
        {id: 1, name: '多选题'},
        {id: 2, name: '判断题'},
        {id: 3, name: '简答题'},
      ],
      search: '',
      delBtlStatu: true,
      total: 0,
      size: 10,
      current: 1,
      subject_list: [],
      tableData: [],
      multipleSelection: [],
    }
  },
  created() {
    this.getTitleList()
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      this.size = val
      this.getTitleList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getTitleList()
    },
    resetForm() {
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm()
    },
    getTitleList() {
      this.$http({
        url: this.$settings.BASE_URL + '/paper/title_list/',
        method: 'get',
        params: {
          page: this.current,
          size: this.size,
          search: this.search,
          title_type: this.title_type,
          subject: this.subject,
        },
      }).then(res => {
        if (res.data.code === 20000) {
          this.tableData = res.data.result.results
          this.total = res.data.result.count
        } else {
          this.$message({
            type: 'warning',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
      this.$http({
        url: this.$settings.BASE_URL + '/paper/subject_list/',
        method: 'get',
      }).then(res => {
        this.subject_list = res.data.result.results
      })
    },
    editHandle(id, titleType) {
      if (titleType === 3) {
        this.$router.push(`/teacher/ShortAnswer/?id=${id}`)
      } else if (titleType === 2) {
        this.$router.push(`/teacher/JudgeTitle/?id=${id}`)
      } else if (titleType === 0) {
        this.$router.push(`/teacher/SingleChoice/?id=${id}`)
      } else if (titleType === 1) {
        this.$router.push(`/teacher/MultiChoice/?id=${id}`)
      }
    },
    delHandle(id) {
      let ids = []
      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      console.log(ids)
      this.$http({
        url: this.$settings.BASE_URL + '/paper/title_edit/del_title/',
        method: 'post',
        data: ids,
      }).then(res => {
        if (res.data.code === 20000) {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'success',
          });
          this.getTitleList()
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    showQuestion(id) {
      this.questionShow.dialog = true
      this.questionShow.loading = true
      this.$http({
        url: this.$settings.BASE_URL + `/paper/title_list/${id}`,
        method: 'get',
      }).then(res => {
        this.questionShow.qType = res.data.result.title_type
        this.questionShow.question = res.data.result
        this.questionShow.loading = false
      })
    },
  }
}
</script>

<style scoped>

</style>
